package pro.softzhang.algo.lc1300;

import pro.softzhang.algo.flag.knowledge.KSlidingWindow;

/**
 * 1343 子数组大小为k且平均值大于等于阈值的数目
 * https://leetcode.cn/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/
 */
public class LC1343_NumberOfSubArraysOfSizeKAndAverageGreaterThanOrEqualToThreshold {
    public static void main(String[] args) {
        int[] arr = {2, 2, 2, 2, 5, 5, 5, 8};
        int k = 3, threshold = 4;
        System.out.println(new Solution().numOfSubarrays(arr, k, threshold));
    }

    @KSlidingWindow(KSlidingWindow.WindowType.FIXED_LENGTH)
    static
    class Solution {
        public int numOfSubarrays(int[] arr, int k, int threshold) {
            int num = 0, sum = 0;
            for (int i = 0; i < arr.length; i++) {
                // 进窗
                sum += arr[i];
                if (i < k - 1) {
                    continue;
                }

                // 更新
                if (sum >= threshold * k) {
                    num++;
                }

                sum -= arr[i - k + 1];
            }
            return num;
        }
    }
}
